<template>
  <div class="container">
    <!-- 搜索区 -->
    <el-form ref="ruleform" :model="ruleform" :inline="true" class="form" label-width="60px">
      <el-form-item label="用户名" prop="batch">
        <el-input v-model="ruleform.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="手机号码" prop="dayAge" label-width="80px">
        <el-input v-model="ruleform.phone" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="$refs['ruleform'].resetFields()">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" @click="editOrAdd('add')">新增用户</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" height="500" border style="width: 80%">
      <el-table-column prop="username" label="用户名" width="150" align="center" />
      <el-table-column prop="phone" label="手机号" width="200" align="center" />
      <el-table-column prop="account" label="账号" align="center" />
      <el-table-column prop="password" label="密码" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editOrAdd('edit', scope)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteFn(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹框 -->
    <EditOrAdd ref="dialog" :type="type" @submit="submitFn" />
  </div>
</template>

<script>
import EditOrAdd from '@/components/EditOrAdd/user'
export default {
  name: 'UserMagage',
  components: { EditOrAdd },
  data() {
    return {
      type: '',
      index: '',
      ruleform: {
        username: '',
        phone: ''
      },
      tableData: [{
        username: '超级管理员',
        phone: '13800000001',
        account: 'admin',
        password: '123456'
      }, {
        username: '用户1',
        phone: '13800000002',
        account: 'user1',
        password: '111111'
      },
      {
        username: '用户2',
        phone: '13800000004',
        account: 'user2',
        password: '666666'
      }]
    }
  },
  methods: {
    // 添加或编辑
    editOrAdd(type, { row, $index } = {}) {
      this.$refs.dialog.centerDialogVisible = true
      this.type = type
      this.index = $index
      if (type === 'edit') {
        this.$refs.dialog.ruleform = { ...row }
      } else {
        this.$refs.dialog.ruleform = { batch: '', dayAge: '' }
        this.$refs.dialog.$refs.form.resetFields()
      }
    },
    // 提交确认
    submitFn(formData) {
      if (this.type === 'edit') {
        this.$set(this.tableData, this.index, formData)
      } else {
        this.tableData.push(formData)
      }
      this.$refs.dialog.centerDialogVisible = false
    },
    // 删除
    deleteFn({ $index }) {
      this.$confirm('确认删除当前数据?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData.splice($index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  padding: 5px;

  /* background-color: rgb(240, 242, 245); */
  .form {
    height: 40px;
    margin: 10px 0;
  }

}
</style>
